/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
// module
import Taro, { Component } from '@tarojs/taro'
import { Swiper, Image } from '@tarojs/components'
import { AtActionSheet, AtActionSheetItem } from "taro-ui"

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http'
import Util from '../../project/func/util';

// 引用组件
import { Addcbtn, Cbtn, Chead, Destop, Editcunit, Ibottom, Ibottomp, Listunitone, Listunittwo, Listunitthree, LogoLine, LogoLinep, Tabheader } from '../../project/comp'

/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class ArticleDetail extends Component {

	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
            // 获取屏幕高度
            scrollHeight: wx.getSystemInfoSync().windowHeight,
            is_master:false,
            data:{},
            set_show:false,
            // type 为是课堂或者文章
            type: this.$router.params.type

		}
    }
    
    /*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/
	//获取信息
	getdata(id, callback){

		Http.AuthOk((res)=>{
			this.setState({
				is_master:res
			})
		})


        if(this.state.type == 'class'){

            Http.Loading({
                url:'/class/detail.php',
                data:{
                    id:id,
                },
                success:(res)=>{
                    this.setState({
                        data:res,
                    })
                    if(callback){
                        callback();
                    }
                }
            })

        }else{

            Http.Loading({
                url:'/article/detail.php',
                data:{
                    id:id,
                },
                success:(res)=>{
                    this.setState({
                        data:res,
                    })
                    if(callback){
                        callback();
                    }
                }
            })

        }
	}

	 componentWillMount () {
		this.getdata(this.$router.params.id);
	 }	

    componentDidMount() {
		this.getdata(this.$router.params.id);
    }

    componentDidShow(){
        this.getdata(this.$router.params.id);
    }

  // onRedirect
	onRedirect(url){
		wx.redirectTo({
			url: url
		})
	}	

	// onNavigate
	onNavigate(url){
		wx.navigateTo({
			url: url
		})
	}

	//onNavigateBack
	onNavigateBack(num=1){
		wx.navigateBack({
			delta: num
		})                    
    }
    

    //收藏
    onCollect(){
        let post_url = "";
        let show_str = "";
        if(!this.state.data.collected){
            post_url = "collect/logic/add.php";
            show_str = "收藏成功";
        }else{
            post_url = "collect/logic/cancel.php";
            show_str = "取消收藏成功";
        }
        Http.post({
            url:post_url,
            data:{
                enum_type: this.state.type == 'class' ? "课堂" : "动态",
                fk_id:this.state.data.id,
            },
            success:(res) => {
                this.getdata(this.state.data.id,()=>{
                    Toast.success(show_str)
                });
            }
        })
    }

    //打开操作
    onDosome(){
        this.setState({
            set_show:true,
        })
    }

    //取消操作
    onCancel(){
        this.setState({
            set_show:false,
        })
    }

    //删除
    delete(){
        Http.post({
            url: this.state.type == 'class' ? 'class/logic/delete.php' : 'article/logic/delete.php',
            data:{
                id: this.state.data.id,
            },
            success:(res)=>{
                Toast.success("删除成功");
                setTimeout(()=>{
                    this.onNavigateBack();
                },500)
            }
        })
    }

    //设置可见性
    seeOK(){
        let seebool = this.state.data.set_authority?0:1;
        Http.post({
            url: this.state.type == 'class' ? 'class/logic/changeAuth.php' : 'article/logic/changeAuth.php',
            data:{
                id: this.state.data.id,
                set_authority:seebool,
            },
            success:(res)=>{
                this.getdata(this.state.data.id, ()=>{
                    Toast.success("设置成功");
                })
                this.setState({
                    set_show:false,
                })        
            }
        })
    }

    //编辑
    Edit(url){
        this.onNavigate(url);
        this.onCancel();
    }

	/**
	 * 渲染
	 */
	render() {
		// 渲染
		return (
            <CPage>
                
                {
                    this.state.is_master ?
                    <Chead onDosome={ this.onDosome }></Chead>
                    :
                    <view></view>
                }
                {
                    this.state.is_master ?
                    <CHand class="f1" h="50"></CHand>
                    :
                    <view></view>
                }
               
                <CContent mb='50'>
                    <CColumn bg='#fff' px='10' py="10">
                        {/* 文章内容部分 */}
                        <CHand size='20' mt='20' bold color='#3c3c3c'>{ this.state.data.set_title }</CHand>
                        <CHand my='14'>
                            <CHand color='#a6a6a6' size='13'>{ Util.formatTime(this.state.data.created,'Y-M-D') }</CHand>
                            {/* <CHand color='#a6a6a6' size='13' ml='14'>李山</CHand> */}
                            <CHand color='#a6a6a6' size='13' ml='14'>阅读{ this.state.data.count_visit }</CHand>
                        </CHand>



                        {
                            this.state.data.object.set_content.map((item, key) => {
                                return(  
                                    
                                    item.type=="image"?
                                    <CHand py='20'>
                                        <image src={ item.content } style="width:690rpx"  mode="widthFix"/>
                                    </CHand>
                                    :
                                    item.type=="text"?
                                    <CHand size='16' color='#9b9b9b' lh='26'>
                                    { item.content }
                                    </CHand>
                                    :
                                    <CHand py='20'>
                                        <video style="width:720rpx; height:380rpx;" src={ item.content }  enable-danmu danmu-btn controls></video>
                                    </CHand>
                                
                                )
                                
                            })
                        }
            



                        <CHand mt='40'>
                            <CHand size='13' color='#aeaeae'>阅读{ this.state.data.count_visit }</CHand>
                            <CSvg color="#aeaeae"  icon={Icon.good} ml='20' w='18' h='18'></CSvg>
                            <CHand size='13' ml='6' color='#aeaeae'>{ this.state.data.count_like }</CHand>
                        </CHand>
                        
                    </CColumn>
                </CContent>
                {/* 底部 */}
                <Ibottom 
                iscollect={ this.state.data.collected }
                onCollect={ this.onCollect }
                ></Ibottom>


                <AtActionSheet isOpened={ this.state.set_show }  onClose={this.onCancel} cancelText='取消' title="操作">



                    <AtActionSheetItem>
                        <CItemX x='center' y='center' onClick={ this.Edit.bind(this, '/screen/add/list?type='+this.state.type+'&id='+this.state.data.id) }>
                        <CSvg icon={Icon.edit} w='25' h='25' color='#7b7b7b' mr='6'></CSvg>
                        <CText size='16' color='#2e2e2e'>编辑</CText>
                        </CItemX>
                    </AtActionSheetItem>


                    <AtActionSheetItem onClick={ this.delete }>
                        <CItemX x='center' y='center'>
                        <CSvg icon={Icon.lajit} w='25' h='25' color='#7b7b7b' mr='6'></CSvg>
                        <CText size='16' color='#2e2e2e'>删除</CText>
                        </CItemX>
                    </AtActionSheetItem>


                    <AtActionSheetItem onClick={ this.seeOK }>
                        <CItemX x='center' y='center'>
                        <CSvg icon={Icon.eyead} w='25' h='25' color='#7b7b7b' mr='6'></CSvg>
                        {
                            this.state.data.set_authority?
                            <CHand size='16' color='#2e2e2e'>设置不可见</CHand>
                            :
                            <CHand size='16' color='#2e2e2e'>设置可见</CHand>
                        }
                        </CItemX>
                    </AtActionSheetItem>
                </AtActionSheet>


            </CPage>
		);
	}
};